home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / zd86_10.zip / REVERSE.ASM < prev    next >
Assembly Source File  |  1991-03-20  |  4KB  |  137 lines

  1. ; Reverse.asm, a sample program for ZD86
  2. ;
  3. ; This program takes the command line, reverses the characters, and then
  4. ; displays the reversed line on the standard output.
  5. ;
  6. ; To assemble and create a .Z86 file for reverse:
  7. ;
  8. ; With A86
  9. ; -----------------------
  10. ; A86 -S reverse.asm
  11. ; Sym-Z86 reverse
  12. ; -----------------------
  13. ; With TASM
  14. ; -----------------------
  15. ; TASM /l reverse
  16. ; TLINK /t reverse
  17. ; Lst-Z86 reverse
  18. ; -----------------------
  19. ; With MASM
  20. ; -----------------------
  21. ; MASM reverse,,reverse.lst;
  22. ; LINK reverse;
  23. ; EXE2BIN reverse.exe reverse.com
  24. ; Lst-Z86 reverse
  25.  
  26. Code_seg  SEGMENT
  27.  
  28. StdOut              EQU       1
  29.  
  30. Backspace           EQU       8
  31. LF                  EQU       10
  32. CR                  EQU       13
  33. EOF                 EQU       26
  34.  
  35.           ORG       80h
  36.  
  37.           Command_line        DB        ?         ; Length byte of command line
  38.           Line                DB        ?         ; Start of command line
  39.  
  40.           ORG       100h
  41.  
  42.           ASSUME    CS:Code_seg,DS:Code_seg,ES:Code_seg,SS:Code_seg
  43. Main:
  44.           JMP       Past_data
  45.  
  46.           ; If someone types "type reverse.com" from DOS
  47.           ; the following will display a useful message rather than garbage.
  48.  
  49.                               DB        3 DUP (Backspace)
  50.           Help_msg            DB        "Usage: Reverse (text to reverse)"
  51.           End_help_msg        DB        EOF
  52.  
  53.  
  54. Past_data:
  55.  
  56. ;         Here is the code.  First, there is a check if any characters
  57. ;         were typed on the command line.  If not, then a help message is
  58. ;         printed.  Otherwise, the command line is printed reversed.
  59. ;
  60. ;         Note: Purposefully, this is not streamlined code.  It is kept
  61. ;         fairly simple, to make it easy to understand as quickly as possible.
  62.  
  63.           MOV       CL,byte ptr [Command_line]
  64.           XOR       CH,CH
  65.           JCXZ      No_command_line
  66.  
  67.           MOV       SI,OFFSET Line
  68.           MOV       DI,OFFSET Reverse_line
  69.           REP       MOVSB                         ; copy text to own location
  70.  
  71.           MOV       DX,DI                         ; points to right after line
  72.           CALL      Print_line_reversed           ; just line this proc wants
  73.  
  74. CRLF_and_quit:
  75.           MOV       AH,6                          ; direct console output
  76.           MOV       DL,CR                         ; print Carriage Return
  77.           INT       21h
  78.           MOV       DL,LF                         ; plus Line Feed
  79.           INT       21h
  80.  
  81. Exit:
  82.           INT       20h
  83.  
  84. No_command_line:
  85.           MOV       End_help_msg,"$"              ; DOS print string ends w/"$"
  86.           MOV       DX,OFFSET Help_msg
  87.           MOV       AH,9
  88.           INT       21h                           ; print string at DS:DX
  89.           JMP       CRLF_and_quit
  90. ;------------------------------------------------------------------------------
  91. Print_line_reversed:
  92. ;Pre : DX -> first char AFTER line, Reverse_line -> first char in line
  93. ;Post: AX, BX, CX, DX unchanged
  94.  
  95.           PUSH      AX
  96.           PUSH      BX
  97.           PUSH      CX
  98.           PUSH      DX
  99.  
  100.           MOV       DI,DX
  101.           DEC       DI                            ; start at last char
  102.           MOV       SI,OFFSET Reverse_line        ; and first char
  103.           SUB       DX,SI                         ; DX now has length
  104.           MOV       CX,DX                         ; since reversing requires
  105.           SHR       CX,1                          ; going 1/2 way from end and
  106.  
  107. Do_reverse_line:
  108.           LODSB                                   ; AL <- DS:[SI] INC SI
  109.           MOV       AH,byte ptr [DI]
  110.           MOV       byte ptr [SI-1],AH
  111.           MOV       byte ptr [DI],AL
  112.           DEC       DI
  113.           LOOP      Do_reverse_line
  114.  
  115. Print_line:
  116.           MOV       AH,40h                        ; Write to file/device
  117.           MOV       BX,StdOut                     ; DX still has length of line
  118.           MOV       CX,DX                         ; which CX needs
  119.           MOV       DX,OFFSET Reverse_line
  120.           INT       21h
  121.  
  122. Quit_reversed:
  123.  
  124.           POP       DX
  125.           POP       CX
  126.           POP       BX
  127.           POP       AX
  128.  
  129.           RET
  130. ;------------------------------------------------------------------------------
  131.  
  132. Reverse_line:                           ; use the area after the .COM file
  133.  
  134. Code_seg  ENDS
  135.  
  136.           END       Main
  137.